package org.handover.controller.admin.userGroup;

import org.handover.domain.AdminValidated;
import org.handover.domain.UserGroup;
import org.handover.repository.UserGroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/admin/userGroup/")
@PreAuthorize("hasAnyRole('root','admin')")
public class UserGroupController {

    private final UserGroupRepository userGroupRepository;

    @Autowired
    public UserGroupController(UserGroupRepository userGroupRepository) {
        this.userGroupRepository = userGroupRepository;
    }

    @GetMapping("list")
    public String list(Model model) {
        model.addAttribute("userGroupList", userGroupRepository.findAll(Sort.by(Sort.Direction.ASC, "no")));
        return "admin/userGroup/list";
    }

    @GetMapping("edit")
    public String getEditUserInformation(Model model){
        model.addAttribute("userGroup", new UserGroup());
        return "admin/userGroup/edit";
    }

    @GetMapping("edit/{id}")
    public String toEdit(@PathVariable Long id, Model model) {
        model.addAttribute("userGroup", userGroupRepository.getOne(id));
        return "admin/userGroup/edit";
    }

    @PostMapping("edit")
    @Transactional
    public String edit(@Validated(value = {AdminValidated.class}) UserGroup userGroup, BindingResult bindingResult, RedirectAttributes model) {
        if (bindingResult.hasErrors()) {
            return "admin/userGroup/edit";
        }
        if (!userGroupNoValidate(userGroup.getId(), userGroup.getNo())){
            bindingResult.rejectValue("no", "userGroup.no.exist", "店号已存在");
            return "admin/user/edit";
        }
        if (!userGroupNameValidate(userGroup.getId(), userGroup.getName())){
            bindingResult.rejectValue("name", "userGroup.name.exist", "店名已存在");
            return "admin/user/edit";
        }
        userGroupRepository.save(userGroup);
        model.addFlashAttribute("message","保存成功");
        return "redirect:/admin/userGroup/list";
    }

    @RequestMapping("delete/{id}")
    @Transactional
    public String delete(@PathVariable Long id, RedirectAttributes model) {
        UserGroup userGroup = userGroupRepository.getOne(id);
        if (userGroup.getUserList().isEmpty()) {
            userGroupRepository.delete(userGroup);
            model.addFlashAttribute("message","删除成功");
        } else{
            model.addFlashAttribute("errorMessage","此分店有员工不允许删除");
        }
        return "redirect:/admin/userGroup/list";
    }

    @GetMapping("userGroupNoValidate")
    @ResponseBody
    public boolean userGroupNoValidate(Long id, String no) {
        return null == id ? !userGroupRepository.existsByNo(no) : !userGroupRepository.existsByIdNotAndNo(id, no);
    }

    @GetMapping("userGroupNameValidate")
    @ResponseBody
    public boolean userGroupNameValidate(Long id, String name) {
        return null == id ? !userGroupRepository.existsByName(name) : !userGroupRepository.existsByIdNotAndName(id, name);
    }
}
